package com.itheima.question.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.PageVo;
import com.itheima.admin.dto.PageDto;
import com.itheima.admin.pojo.Dept;
import com.itheima.admin.pojo.User;
import com.itheima.admin.vo.DeptVo;
import com.itheima.admin.vo.UserPageVo;
import com.itheima.common.contants.RedisPicConstans;
import com.itheima.question.dto.QuestionDto;
import com.itheima.question.mapper.QuestionMapper;
import com.itheima.question.pojo.Question;
import com.itheima.question.service.IQuestionService;
import com.itheima.question.vo.QuestionPageVo;
import com.itheima.question.vo.QuestionVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class QuestionServiceImpl extends ServiceImpl<QuestionMapper,Question> implements IQuestionService {
    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${fileServerUrl}")
    private String fileServerUrl;

    @Override
    public boolean addQuestion(QuestionDto questionDto) {
        Question question = new Question();
        BeanUtils.copyProperties(questionDto,question);
        int insert = questionMapper.insert(question);

        //将问题id存入redis的set集合中，用于动态审核
        String id = question.getId();
        redisTemplate.opsForSet().add(RedisPicConstans.AUDIT_QUESTION,id);

        //保存图片id到redis中，清理垃圾图片使用的
        String pid = question.getPicture();
        redisTemplate.opsForSet().add(RedisPicConstans.USE_PIC,pid.replace(fileServerUrl,""));
        return insert>0;
    }
    @Override
    public PageVo<QuestionPageVo> queryByPage(PageDto pageDto) {

        //设置分页参数
        PageHelper.startPage(pageDto.getCurrentPage(),pageDto.getPageSize());


        List<Question> list = questionMapper.selectByPage(pageDto.getStatus(),pageDto.getQueryString());
        PageInfo<Question> pageInfo = new PageInfo<>(list);

        //List<User>  ==> List<UserPageVo>
        List<QuestionPageVo> collect = pageInfo.getList().stream().map(question -> {
            QuestionPageVo questionPageVo = new QuestionPageVo();
            BeanUtils.copyProperties(question, questionPageVo);
            questionPageVo.setState(question.getState());
            return questionPageVo;
        }).collect(Collectors.toList());

        return new PageVo<>(collect,Long.valueOf(pageInfo.getTotal()).intValue());
    }

    @Override
    public QuestionVo queryById(String id) {
        Question question = questionMapper.selectById(id);
        QuestionVo questionVo = new QuestionVo();
        if(question!= null){
            BeanUtils.copyProperties(question,questionVo);
        }
        return questionVo;
    }
}
